<?php 
    
namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use Hash;  
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CarController extends Controller
{

  public function getAdd(Request $request)
  { 
         $id=isset($_GET['id'])?$_GET['id']:'0';//商品的id
  
        $uid =  session()->get('username')->id; 

       $num=$_GET['num']; //var_dump($num);//商品的数量
        //获取数据源 
        $res =DB::table('project_goods')->where('id',$id)->first();
        //添加一个商品数量
        $res ->m =$num;   

        //执行相应的操作
       if(session()->has("shoplist.{$id}")){

                //商品数量加以一
                $shoplist = session()->get("shoplist.{$id}");

                $shoplist->m = $num ;
                //把商品放入session()中
                session()->put("shoplist.{$id}",$shoplist);
                  //var_dump(session()->get('shoplist')); die;
              }else{
                $shoplist =$res;


                session()->put("shoplist.{$id}",$shoplist);
               
                //把商品放入session()中
                
          
            } 
            //var_dump(session()->get('shoplist'));die;

            $row =array();
          //将存入session的数据写入购物车
          $row['gid']=$res->id; $row['uid']= $uid;$row['goodsname']=$res->goodsname; $row['tid']=$res->tid;
          $row['goodspic']=$res->goodspic; $row['goodsprice']=$res->goodsprice;
           $row['goodsnum']=$res->goodsnum;$row['m']=$res->m;
            
           $re=   DB::table('project_cart')->insert($row);
           if($re){
             return view('home/car/show');  
           }
      

      //删除address中对应的地址 $res=session()->get("address"); unset($res['0']); session()->put('address',$res); // var_dump($res);die;var_dump(session()->get('shoplist'));die;      
               
  }

  //ajax改变数据
  public function getChange()
    { 

      //获取所有加入s购物车商品的信息
       $id =$_GET['id'];
       $v =$_GET['v']; 
      
       //改变商品对应的数量
        $shoplist = session()->get("shoplist.{$id}");
        $shoplist->m=$v;
         
    }

    //获取要删除的购物车

     public function getDel($id)
    { 
  // 获取所有加入s购物车商品的信息
        $res=session()->get("shoplist");
        //获取要删除的id 
        //var_dump($res);
        unset($res[$id]);
        session()->put('shoplist',$res);
      
      return view('home/car/show');
    }

}

